#include "stdio.h"
#include "CommsHandler.h"
#include "HostInterfaceActivator.h"
#include "UCLActivator.h"

CommsHandler::CommsHandler() {
    hostInterface = NULL;
}

HostInterfaceActivator* CommsHandler::getHostInterface() {
    return hostInterface;
}

void CommsHandler::setHostInterface(HostInterfaceActivator* host) {
    hostInterface = host;
}

UCLActivator* CommsHandler::getUCL() {
    return ucl;
}

void CommsHandler::setUCL(UCLActivator* uact) {
    ucl = uact;
}

int CommsHandler::attemptOutwardCommunication() {
    int ret;

    inward = 0;
    if (checkIfDemoMode() == TRUE)
        ret = performDemoMode();
    else {
        initConnectSequence();
        if ((ret = checkStatusPreComms()) == CONTINUE_PROCESS) {
            // no offline auth
            if (hostInterface == NULL) {
                errorMessage = "No HostInterface";
                ret = ERROR_OCCURRED;
            } else {
                if ((errorMessage = hostInterface->setupForComms()) == NULL)
                    do {
                        if ((ret = attemptConnect()) == TRUE) {
                            if ((ret = checkConnectionStatus()) == TRUE) {
                                if ((errorMessage = performLogOn()) == NULL) {
                                    if ((errorMessage = performHostInterface()) == NULL)
                                        errorMessage = performLogOff();
                                }
                            }
                            if (errorMessage != NULL)
                                ret = ERROR_OCCURRED;
                        }
                        disConnect();
                    } while (ret != TRUE && isRetryConnect() == TRUE);
            }
        }
    }
    if (hostInterface != NULL) {
        if (ret != TRUE)
            ret = hostInterface->errorOccurred(ret);
        else
            ret = hostInterface->noErrorOccurred();
        ret = checkStatusPostComms(ret);
    }
    hostInterface = NULL;
    return ret;
}

int CommsHandler::attemptInwardCommunication() {
    int ret;

    inward = 1;
    if (checkIfDemoMode() == TRUE)
        ret = performDemoMode();
    else {
        if ((ret = checkStatusPreCommsInward()) == CONTINUE_PROCESS) {
            // no offline auth
            if (hostInterface == NULL) {
                errorMessage = "No HostInterface";
                ret = ERROR_OCCURRED;
            } else {
                if ((errorMessage = hostInterface->setupForComms()) == NULL) {
                    if ((ret = checkInwardConnectionStatus()) == TRUE) {
                        if ((errorMessage = performLogOn()) == NULL) {
                            if ((errorMessage = performHostInterface()) == NULL)
                                errorMessage = performLogOff();
                        }
                    }
                    disConnectInwards();
                }
                if (errorMessage != NULL)
                    ret = ERROR_OCCURRED;
            }
        }
    }
    if (hostInterface != NULL) {
        if (ret != TRUE)
            ret = hostInterface->errorOccurred(ret);
        else
            ret = hostInterface->noErrorOccurred();
        ret = checkStatusPostComms(ret);
    }
    hostInterface = NULL;
    return ret;
}


const char* CommsHandler::performHostInterface() {
    return hostInterface->performHostInterface();
}

char* CommsHandler::performLogOn() {
    return hostInterface->performLogOn();
}

char* CommsHandler::performLogOff() {
    return hostInterface->performLogOff();
}
